﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using API.Models;
using PPG.DataAccess;
using PPG.CryptoProvider;
using System.Xml;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections;

namespace API.Controllers
{
    public class UserReferralsController : ApiController
    {
        DataSourceCommunicator dsComm = new DataSourceCommunicator(DataSourceCommunicator.ParamType.ServerCredentials, ConfigurationManager.AppSettings["DBServerName"].ToString(), ConfigurationManager.AppSettings["DBUserName"].ToString(), ConfigurationManager.AppSettings["DBPassword"].ToString());

        public HttpResponseMessage OptionsValidateUserLogin()
        {
            var response = new HttpResponseMessage();
            response.StatusCode = HttpStatusCode.OK;
            return response;
        }

        public UserReferralResponse GetUserReferrals(int id)
        {
            UserReferralResponse response = new UserReferralResponse();
            dsComm.AddParameter("@ParamLoginID", id);
            string data = dsComm.ExecuteStoredProcedure("spGetReferralsForUser");
            XmlTextReader xReader = new XmlTextReader(data, XmlNodeType.Document, null);
            DataSet UserDataSet = new DataSet();
            UserDataSet.ReadXml(xReader);

            DataTable UserCategories = null;
            DataTable NewRequests = null;
            DataTable CompletedRequest = null;
            DataTable InProcessRequest = null;
            DataTable ScheduledRequest = null; 

            if (UserDataSet.Tables.Count > 0)
            {
                if (UserDataSet.Tables[0] != null)
                {
                    UserCategories = new DataTable();
                    UserCategories = UserDataSet.Tables[0];
                    response.ReferralCategories = UserCategories;
                }
            }

            if (UserDataSet.Tables.Count > 1)
            {
                if (UserDataSet.Tables[1] != null)
                {
                    NewRequests = new DataTable();
                    NewRequests = UserDataSet.Tables[1];
                    response.NewRequests = NewRequests;
                }
            }
            if (UserDataSet.Tables.Count > 2)
            {
                if (UserDataSet.Tables[2] != null)
                {
                    CompletedRequest = new DataTable();
                    CompletedRequest = UserDataSet.Tables[2];
                    response.CompletedRequests = CompletedRequest;
                }
            }
            if (UserDataSet.Tables.Count > 3)
            {
                if (UserDataSet.Tables[3] != null)
                {
                    ScheduledRequest = new DataTable();
                    ScheduledRequest = UserDataSet.Tables[3];
                    response.ScheduledRequests = ScheduledRequest;
                }

            }

            if (UserDataSet.Tables.Count > 4)
            {
                if (UserDataSet.Tables[4] != null)
                {
                    InProcessRequest = new DataTable();
                    InProcessRequest = UserDataSet.Tables[4];
                    response.InProcessRequests = InProcessRequest;
                }

            }

            if (UserDataSet.Tables.Count > 5)
            {
                if (UserDataSet.Tables[5] != null)
                {
                    foreach (DataRow dr in UserDataSet.Tables[5].Rows)
                    {
                        ArrayList ar = new ArrayList();
                        ar.Add(dr[0]);
                        ar.Add(int.Parse(dr[1].ToString()));
                        response.WeeklyStats.Add(ar);
                    }

                }

            }


            //******************* TODAY'S STATS *******************//
            if (UserDataSet.Tables.Count > 6)
            {
                if (UserDataSet.Tables[6] != null)
                {
                    foreach (DataRow dr in UserDataSet.Tables[6].Rows)
                    {
                        if (!String.IsNullOrEmpty(dr[0].ToString()))
                        {
                            ArrayList ar = new ArrayList();
                            ar.Add(dr[0]);
                            ar.Add(int.Parse(dr[1].ToString()));
                            response.DailyPracticeSplit.Add(ar);
                        }
                    }
                }

            }

            if (UserDataSet.Tables.Count > 7)
            {
                if (UserDataSet.Tables[7] != null)
                {
                    foreach (DataRow dr in UserDataSet.Tables[7].Rows)
                    {
                        if (!String.IsNullOrEmpty(dr[0].ToString()))
                        {
                            ArrayList ar = new ArrayList();
                            ar.Add(dr[0]);
                            ar.Add(int.Parse(dr[1].ToString()));
                            response.DailyReferralInStat.Add(ar);
                        }
                    }
                }

            }

            if (UserDataSet.Tables.Count > 8)
            {
                if (UserDataSet.Tables[8] != null)
                {

                    foreach (DataRow dr in UserDataSet.Tables[8].Rows)
                    {
                        if (!String.IsNullOrEmpty(dr[0].ToString()))
                        {
                            ArrayList ar = new ArrayList();
                            ar.Add(dr[0]);
                            ar.Add(int.Parse(dr[1].ToString()));
                            response.DailyReferralOutStat.Add(ar);
                        }
                    }
                }

            }

            //******************* YESTERDAYS'S STATS *******************//
            if (UserDataSet.Tables.Count > 9)
            {
                if (UserDataSet.Tables[9] != null)
                {
                    foreach (DataRow dr in UserDataSet.Tables[9].Rows)
                    {
                        if (!String.IsNullOrEmpty(dr[0].ToString()))
                        {
                            ArrayList ar = new ArrayList();
                            ar.Add(dr[0]);
                            ar.Add(int.Parse(dr[1].ToString()));
                            response.YesterdayPracticeSplit.Add(ar);
                        }
                    }
                }

            }

            if (UserDataSet.Tables.Count > 10)
            {
                if (UserDataSet.Tables[10] != null)
                {
                    foreach (DataRow dr in UserDataSet.Tables[10].Rows)
                    {
                        if (!String.IsNullOrEmpty(dr[0].ToString()))
                        {
                            ArrayList ar = new ArrayList();
                            ar.Add(dr[0]);
                            ar.Add(int.Parse(dr[1].ToString()));
                            response.YesterdayReferralInStat.Add(ar);
                        }
                    }
                }

            }

            if (UserDataSet.Tables.Count > 11)
            {
                if (UserDataSet.Tables[11] != null)
                {

                    foreach (DataRow dr in UserDataSet.Tables[11].Rows)
                    {
                        if (!String.IsNullOrEmpty(dr[0].ToString()))
                        {
                            ArrayList ar = new ArrayList();
                            ar.Add(dr[0]);
                            ar.Add(int.Parse(dr[1].ToString()));
                            response.YesterdayReferralOutStat.Add(ar);
                        }
                    }
                }

            }

            //******************* WEEKLY STATS *******************//
            if (UserDataSet.Tables.Count > 12)
            {
                if (UserDataSet.Tables[12] != null)
                {
                    foreach (DataRow dr in UserDataSet.Tables[12].Rows)
                    {
                        if (!String.IsNullOrEmpty(dr[0].ToString()))
                        {
                            ArrayList ar = new ArrayList();
                            ar.Add(dr[0]);
                            ar.Add(int.Parse(dr[1].ToString()));
                            response.WeeklyPracticeSplit.Add(ar);
                        }
                    }
                }

            }

            if (UserDataSet.Tables.Count > 13)
            {
                if (UserDataSet.Tables[13] != null)
                {
                    foreach (DataRow dr in UserDataSet.Tables[13].Rows)
                    {
                        if (!String.IsNullOrEmpty(dr[0].ToString()))
                        {
                            ArrayList ar = new ArrayList();
                            ar.Add(dr[0]);
                            ar.Add(int.Parse(dr[1].ToString()));
                            response.WeeklyReferralInStat.Add(ar);
                        }
                    }
                }

            }

            if (UserDataSet.Tables.Count > 14)
            {
                if (UserDataSet.Tables[14] != null)
                {

                    foreach (DataRow dr in UserDataSet.Tables[14].Rows)
                    {
                        if (!String.IsNullOrEmpty(dr[0].ToString()))
                        {
                            ArrayList ar = new ArrayList();
                            ar.Add(dr[0]);
                            ar.Add(int.Parse(dr[1].ToString()));
                            response.WeeklyReferralOutStat.Add(ar);
                        }
                    }
                }

            }


            //******************* MONTHLY STATS *******************//
            if (UserDataSet.Tables.Count > 15)
            {
                if (UserDataSet.Tables[15] != null)
                {
                    foreach (DataRow dr in UserDataSet.Tables[15].Rows)
                    {
                        if (!String.IsNullOrEmpty(dr[0].ToString()))
                        {
                            ArrayList ar = new ArrayList();
                            ar.Add(dr[0]);
                            ar.Add(int.Parse(dr[1].ToString()));
                            response.MonthlyPracticeSplit.Add(ar);
                        }
                    }
                }

            }

            if (UserDataSet.Tables.Count > 16)
            {
                if (UserDataSet.Tables[16] != null)
                {
                    foreach (DataRow dr in UserDataSet.Tables[16].Rows)
                    {
                        if (!String.IsNullOrEmpty(dr[0].ToString()))
                        {
                            ArrayList ar = new ArrayList();
                            ar.Add(dr[0]);
                            ar.Add(int.Parse(dr[1].ToString()));
                            response.MonthlyReferralInStat.Add(ar);
                        }
                    }
                }

            }

            if (UserDataSet.Tables.Count > 17)
            {
                if (UserDataSet.Tables[17] != null)
                {

                    foreach (DataRow dr in UserDataSet.Tables[17].Rows)
                    {
                        if (!String.IsNullOrEmpty(dr[0].ToString()))
                        {
                            ArrayList ar = new ArrayList();
                            ar.Add(dr[0]);
                            ar.Add(int.Parse(dr[1].ToString()));
                            response.MonthlyReferralOutStat.Add(ar);
                        }
                    }
                }

            }

            //******************* PREVIOUS MONTHLY STATS *******************//
            if (UserDataSet.Tables.Count > 18)
            {
                if (UserDataSet.Tables[18] != null)
                {
                    foreach (DataRow dr in UserDataSet.Tables[18].Rows)
                    {
                        if (!String.IsNullOrEmpty(dr[0].ToString()))
                        {
                            ArrayList ar = new ArrayList();
                            ar.Add(dr[0]);
                            ar.Add(int.Parse(dr[1].ToString()));
                            response.PreviousMonthlyPracticeSplit.Add(ar);
                        }
                    }
                }

            }

            if (UserDataSet.Tables.Count > 19)
            {
                if (UserDataSet.Tables[19] != null)
                {
                    foreach (DataRow dr in UserDataSet.Tables[19].Rows)
                    {
                        if (!String.IsNullOrEmpty(dr[0].ToString()))
                        {
                            ArrayList ar = new ArrayList();
                            ar.Add(dr[0]);
                            ar.Add(int.Parse(dr[1].ToString()));
                            response.PreviousMonthlyReferralInStat.Add(ar);
                        }
                    }
                }

            }

            if (UserDataSet.Tables.Count > 20)
            {
                if (UserDataSet.Tables[20] != null)
                {

                    foreach (DataRow dr in UserDataSet.Tables[20].Rows)
                    {
                        if (!String.IsNullOrEmpty(dr[0].ToString()))
                        {
                            ArrayList ar = new ArrayList();
                            ar.Add(dr[0]);
                            ar.Add(int.Parse(dr[1].ToString()));
                            response.PreviousMonthlyReferralOutStat.Add(ar);
                        }
                    }
                }

            }

            return response;
        }
    }
}
